package com.hxqh.bo.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.hxqh.bo.bean.FpsFuturesMarketInfo;

/**
 * A data access object (DAO) providing persistence and search support for
 * FpsFuturesMarketInfo entities. Transaction control of the save(), update()
 * and delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.hxqh.bo.bean.FpsFuturesMarketInfo
 * @author MyEclipse Persistence Tools
 */

public class FpsFuturesMarketInfoDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory
			.getLog(FpsFuturesMarketInfoDAO.class);
	// property constants
	public static final String FUTURES_CODE = "futuresCode";
	public static final String MAX_MARKET_ORDER_VOLUME = "maxMarketOrderVolume";
	public static final String MIN_MARKET_ORDER_VOLUME = "minMarketOrderVolume";
	public static final String VOLUME_MULTIPLE = "volumeMultiple";
	public static final String PRICE_TICK = "priceTick";
	public static final String EXCHANGE_ID = "exchangeId";
	public static final String LAST_PRICE = "lastPrice";
	public static final String UPPER_LIMIT_PRICE = "upperLimitPrice";
	public static final String LOWER_LIMIT_PRICE = "lowerLimitPrice";
	public static final String FPS_FUTURES_MARKET_INFOCOL = "fpsFuturesMarketInfocol";

	protected void initDao() {
		// do nothing
	}
	
	public List getFuturesCode() {
		return getHibernateTemplate().executeFind(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String sql = "select distinct a.FUTURES_CODE from fps_policy_info a " +
						"where a.FUTURES_CODE not in " +
							"(select distinct b.FUTURES_CODE FROM fps_futures_market_info b) " +
							"and a.FUTURES_CODE is not null";
				SQLQuery q = arg0.createSQLQuery(sql);
				q.addScalar("a.FUTURES_CODE", Hibernate.STRING);
				
				return q.list();
			}
			
		});
	}

	public void save(FpsFuturesMarketInfo transientInstance) {
		log.debug("saving FpsFuturesMarketInfo instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	public void update(FpsFuturesMarketInfo transientInstance) {
		log.debug("updating FpsFuturesMarketInfo instance");
		try {
			getHibernateTemplate().update(transientInstance);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}

	public void delete(FpsFuturesMarketInfo persistentInstance) {
		log.debug("deleting FpsFuturesMarketInfo instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public FpsFuturesMarketInfo findById(java.lang.Integer id) {
		log.debug("getting FpsFuturesMarketInfo instance with id: " + id);
		try {
			FpsFuturesMarketInfo instance = (FpsFuturesMarketInfo) getHibernateTemplate()
					.get("com.hxqh.bo.bean.FpsFuturesMarketInfo", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(FpsFuturesMarketInfo instance) {
		log.debug("finding FpsFuturesMarketInfo instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding FpsFuturesMarketInfo instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from FpsFuturesMarketInfo as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByFuturesCode(Object futuresCode) {
		return findByProperty(FUTURES_CODE, futuresCode);
	}

	public List findByMaxMarketOrderVolume(Object maxMarketOrderVolume) {
		return findByProperty(MAX_MARKET_ORDER_VOLUME, maxMarketOrderVolume);
	}

	public List findByMinMarketOrderVolume(Object minMarketOrderVolume) {
		return findByProperty(MIN_MARKET_ORDER_VOLUME, minMarketOrderVolume);
	}

	public List findByVolumeMultiple(Object volumeMultiple) {
		return findByProperty(VOLUME_MULTIPLE, volumeMultiple);
	}

	public List findByPriceTick(Object priceTick) {
		return findByProperty(PRICE_TICK, priceTick);
	}

	public List findByExchangeId(Object exchangeId) {
		return findByProperty(EXCHANGE_ID, exchangeId);
	}

	public List findByLastPrice(Object lastPrice) {
		return findByProperty(LAST_PRICE, lastPrice);
	}

	public List findByUpperLimitPrice(Object upperLimitPrice) {
		return findByProperty(UPPER_LIMIT_PRICE, upperLimitPrice);
	}

	public List findByLowerLimitPrice(Object lowerLimitPrice) {
		return findByProperty(LOWER_LIMIT_PRICE, lowerLimitPrice);
	}

	public List findByFpsFuturesMarketInfocol(Object fpsFuturesMarketInfocol) {
		return findByProperty(FPS_FUTURES_MARKET_INFOCOL,
				fpsFuturesMarketInfocol);
	}

	public List findAll() {
		log.debug("finding all FpsFuturesMarketInfo instances");
		try {
			String queryString = "from FpsFuturesMarketInfo";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public FpsFuturesMarketInfo merge(FpsFuturesMarketInfo detachedInstance) {
		log.debug("merging FpsFuturesMarketInfo instance");
		try {
			FpsFuturesMarketInfo result = (FpsFuturesMarketInfo) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(FpsFuturesMarketInfo instance) {
		log.debug("attaching dirty FpsFuturesMarketInfo instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(FpsFuturesMarketInfo instance) {
		log.debug("attaching clean FpsFuturesMarketInfo instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static FpsFuturesMarketInfoDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (FpsFuturesMarketInfoDAO) ctx.getBean("FpsFuturesMarketInfoDAO");
	}
}